package liangtWorkSpace.validation.advice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName MyAdvice
 * @Description : 功能说明
 * @Author : LiangT
 * @Date : 2020/5/14 22:20
 */

// 指向所有带有注解@RestController的控制器
//@ControllerAdvice(annotations = RestController.class)
//public class AnnotationAdvice {}
//
// 指向所有指定包中的控制器
//@ControllerAdvice("org.example.controllers")
//public class BasePackageAdvice {}
//
// 指向所有带有指定签名的控制器
//@ControllerAdvice(assignableTypes = {ControllerInterface.class, AbstractController.class})
//public class AssignableTypesAdvice {}
@RestControllerAdvice(basePackageClasses = MyTestController.class)
public class MyAdvice {
    private Logger log = LoggerFactory.getLogger(MyAdvice.class);

        /**
         * 全局异常处理
         * @param exception
         * @return
         */
    @ExceptionHandler(value = Exception.class)
    public BaseReturnVO errorExt(Exception exception){
        BaseReturnVO baseReturnVO = new BaseReturnVO();
        if (exception instanceof NullPointerException){
            
        }
        baseReturnVO.setRetCode(8);
        baseReturnVO.setRetMsg("Exception exception");
        System.out.println("尝试获取异常状态");
        return baseReturnVO;
    }

    /**
     * 指定异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = MyException.class)
    public BaseReturnVO a(MyException ex){
        BaseReturnVO baseReturnVO = new BaseReturnVO();
        System.out.println(ex.getMsg());
        baseReturnVO.setRetCode(8);
        baseReturnVO.setRetMsg("MyException.class");
        return baseReturnVO;
    }
}
